package com.ruoyi.by.health.service.impl;

import com.ruoyi.by.health.domain.entity.ByhealthEmpVoteLog;
import com.ruoyi.by.health.domain.entity.ByhealthPlaybill;
import com.ruoyi.by.health.domain.vo.ByhealthEmpVoteLogVo;
import com.ruoyi.by.health.mapper.ByhealthEmpVoteLogMapper;
import com.ruoyi.by.health.mapper.ByhealthPlaybillMapper;
import com.ruoyi.by.health.service.IByhealthEmpVotesService;
import com.ruoyi.by.health.service.IByhealthPlaybillService;
import com.ruoyi.common.core.domain.AjaxResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * @ClassName ByhealthEmpVotesServiceImpl
 * @Author 张敬琦
 * @Description 添加描述
 * @Date 2024/1/19 14:12
 **/
@Slf4j
@Service
public class ByhealthEmpVotesServiceImpl implements IByhealthEmpVotesService {
    @Autowired
    ByhealthEmpVoteLogMapper empVoteLogMapper;
    @Autowired
    IByhealthPlaybillService playbillService;
    @Autowired
    ByhealthPlaybillMapper playbillMapper;

    @Override
    public AjaxResult selectPlaybillList() {
        ByhealthPlaybill playbillQo = new ByhealthPlaybill();

        Integer bossCount = empVoteLogMapper.getBossCount();
        Integer empCount = empVoteLogMapper.getEmpCount();
        List<ByhealthPlaybill> playbillList = playbillService.selectPlaybillList(playbillQo);
        Double temp = 0.00;
        for (int i = 0; i < playbillList.size(); i++) {
            ByhealthPlaybill playbill = playbillList.get(i);
            Integer bossVote = empVoteLogMapper.getBossVoteByPlaybillId(playbill.getId());
            playbill.setBossVote(bossVote);
            Integer empVote = empVoteLogMapper.getEmpVoteByPlaybillId(playbill.getId());
            playbill.setEmpVote(empVote);
            if (i != playbillList.size() - 1) {
                Double playScore = getPlayScore(bossCount, empCount, bossVote, empVote);
                playbill.setPlayScore(String.valueOf(playScore));
                temp = temp + playScore;
            } else {
                if (0.00==temp) {
                    playbill.setPlayScore("0.00");
                } else {
                    if(0.00==temp){
                        playbill.setPlayScore("0.00");
                    }else {
                        BigDecimal two = new BigDecimal(100.00-temp);
                        playbill.setPlayScore(String.valueOf(two.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue()));
                    }
                }
            }
            playbillMapper.updateByPrimaryKeySelective(playbill);
        }
        return AjaxResult.success(playbillList);
    }

    @Override
    public List<ByhealthEmpVoteLog> getEmpVoteLogListByEmpId(String empId) {
        return null;
    }

    @Override
    public List<ByhealthEmpVoteLogVo> getEmpVoteLogList(ByhealthEmpVoteLogVo empVote) {
        return empVoteLogMapper.getEmpVoteLogList(empVote);
    }

    private Double getPlayScore(Integer bossCount, Integer empCount, Integer bossVote, Integer empVote) {
        //log.info("-----统计投票参数bossCount：{},empCount:{},bossVote:{},empVote:{}", bossCount, empCount, bossVote, empVote);
        Double playScore = 0.00;
        if (bossCount == 0 && empCount != 0) {
            playScore = (empVote / Double.valueOf((empCount)) * 100);
            BigDecimal two = new BigDecimal(playScore);
            //log.info("-----统计投票结果playScoreString1:{}", two.toString());
            return two.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        }
        if (empCount == 0 && bossCount != 0) {
            playScore = (bossVote / (Double.valueOf(bossCount)) * 100);
            BigDecimal two = new BigDecimal(playScore);
            //log.info("-----统计投票结果playScoreString2:{}", two.toString());
            return two.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        }
        if (bossCount == 0 && empCount == 0) {
            return 0.00;
        }
        playScore = (((empVote / Double.valueOf(empCount)) * (0.3) + ((bossVote / Double.valueOf(bossCount)) * (0.7)))) * 100;
        BigDecimal two = new BigDecimal(playScore);
        //log.info("-----统计投票结果playScore3:{}", two.toString());
        return two.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();

    }

}
